from PyQt4 import QtCore, QtGui;
import numpy as np;

class TrackPath_Item(object):
    def __init__(self, nTrackId, aFrms, aPos, gScene):
        self.nTrackId = int(nTrackId);
        self.aPos = aPos;
        self.nInit, self.nEnd = aFrms[0], aFrms[-1];
        self.pathItem = QtGui.QGraphicsPathItem();
        self.aC = np.random.randint(50,200,3);
        self.aC = [200,200,200];
        self.pathItem.setPen(QtGui.QPen(QtGui.QColor(self.aC[0],self.aC[1],self.aC[2], 200), 3));
        self.createToken();
        self.createLabel();
        gScene.addItem(self.pathItem);
        gScene.addItem(self.tokenItem);
        gScene.addItem(self.qTxtLbl);

    def createToken(self):
        nS = 8;
        self.tokenItem = QtGui.QGraphicsPathItem();
        token = QtGui.QPainterPath();
        token.addEllipse(-nS/2, -nS/2, nS, nS);
        self.tokenItem.setPath(token);
        self.tokenItem.setPen(QtGui.QPen(QtCore.Qt.black, 1));
        self.tokenItem.setBrush(QtGui.QBrush(QtGui.QColor(self.aC[0],self.aC[1],self.aC[2], 255)));
        self.tokenItem.setVisible(False);
        
    def createLabel(self):
        self.qTxtLbl = QtGui.QGraphicsTextItem();
        self.qTxtLbl.setTextWidth(100);
        sTrkId = str(self.nTrackId);
        self.qTxtLbl.setHtml("<div align='center'>" + sTrkId + "</div>"); 
        self.qTxtLbl.setDefaultTextColor(QtGui.QColor(0,0,0, 255));
        self.qTxtLbl.setFont(QtGui.QFont("Arial", 8, QtGui.QFont.Normal));
        self.qTxtLbl.setVisible(False);
        
    def removeTrack(self, gScene):
        gScene.removeItem(self.pathItem);
        gScene.removeItem(self.tokenItem);
        gScene.removeItem(self.qTxtLbl);
    
    def updateTrackPath(self, nFrm, nWin=20):
        pathTrack = QtGui.QPainterPath();
        if nFrm < self.nInit or nFrm > self.nEnd:
            self.pathItem.setPath(pathTrack);
            self.tokenItem.setVisible(False);
            self.qTxtLbl.setVisible(False);
            return;
        nT = nFrm-self.nInit;
        nInit, nEnd = nT-nWin+1, nT+1;
        nInit = 0 if nInit<0 else nInit;
        aPosW = self.aPos[nInit:nEnd];
        pathTrack.moveTo(aPosW[0,0],aPosW[0,1]);
        for i,(x,y) in enumerate(aPosW):
            if i==0: continue;
            pathTrack.lineTo(x,y);
        self.pathItem.setPath(pathTrack);
        #Make Visible and Position the Token & Label
        self.tokenItem.setVisible(True);
        self.tokenItem.setPos(aPosW[-1,0],aPosW[-1,1]);
        self.qTxtLbl.setVisible(True);
        self.qTxtLbl.setPos(aPosW[-1,0]-50,aPosW[-1,1]);
        
    def setSelectedStyle(self, bSelected):
        if bSelected:
            self.tokenItem.setPen(QtGui.QPen(QtCore.Qt.blue, 2));
            self.qTxtLbl.setDefaultTextColor(QtGui.QColor(0,0,255, 255));
            self.qTxtLbl.setFont(QtGui.QFont("Arial", 8, QtGui.QFont.Bold));
        else:            
            self.tokenItem.setPen(QtGui.QPen(QtCore.Qt.black, 1));         
            self.qTxtLbl.setDefaultTextColor(QtGui.QColor(0,0,0, 255));
            self.qTxtLbl.setFont(QtGui.QFont("Arial", 8, QtGui.QFont.Normal));
        
    def getPos(self, nFrm):
        return self.aPos[nFrm-self.nInit];
